package com.shopsmart.android;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.example.android.nfc.record.NdefMessageParser;
import com.example.android.nfc.record.ParsedNdefRecord;
import com.example.android.nfc.record.TextRecord;
import com.shopsmart.android.entities.DealTagEntity;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class TagTouchActivity extends Activity {

	public static final String TAG = TagTouchActivity.class.getSimpleName();

	private ArrayList<DealTagEntity> dealTagsList = new ArrayList<DealTagEntity>();
	private ListView dealsListView = null;
	private AllDealsAdapter dealsAdapter = new AllDealsAdapter();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		dealsListView = (ListView) findViewById(R.id.homeAllTagsLV);
		dealsListView.setAdapter(dealsAdapter);
	}
	
	

	@Override
	protected void onResume() {
		super.onResume();
		resolveIntent(getIntent());
	}

	void resolveIntent(Intent intent) {
		// Parse the intent
		String action = intent.getAction();
		if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(action) || NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
			
			Tag tagFromNFC = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
			Log.i(TAG, "NFC Tag info : " + tagFromNFC.toString());
			Log.i(TAG, "NFC Tag info : " + tagFromNFC.getId());
			Log.i(TAG, "NFC Tag info : " + ByteArrayToHexString(tagFromNFC.getId()));
			
			byte[] tagIDFromNFC = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
			Log.i(TAG, "NFC Tag ID hex  : " + ByteArrayToHexString(tagIDFromNFC));
			
			
			Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
			NdefMessage[] msgs;
	        if (rawMsgs != null) {
	            msgs = new NdefMessage[rawMsgs.length];
	            for (int i = 0; i < rawMsgs.length; i++) {
	                msgs[i] = (NdefMessage) rawMsgs[i];
	            }
	        }
			
			// TODO fetch all deals from kinvey to dealTagsList
			dealTagsList.add(new DealTagEntity("101", "Gap 20%", Calendar.getInstance().getTimeInMillis()));
			dealTagsList.add(new DealTagEntity("102", "Levis 25%", Calendar.getInstance().getTimeInMillis()));
			dealTagsList.add(new DealTagEntity("103", "Aeropostale 30%", Calendar.getInstance().getTimeInMillis()));
			dealTagsList.add(new DealTagEntity("104", "Perry Elis 10%", Calendar.getInstance().getTimeInMillis()));
			dealTagsList.add(new DealTagEntity("105", "Ralph Lauren 5%", Calendar.getInstance().getTimeInMillis()));
			dealsAdapter.notifyDataSetChanged();
		}
	}
	
	String ByteArrayToHexString(byte [] inarray){
        int i, j, in;
        String [] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};
        String out= "";
   
        for(j = 0 ; j < inarray.length ; ++j)
            {
            in = (int) inarray[j] & 0xff;
            i = (in >> 4) & 0x0f;
            out += hex[i];
            i = in & 0x0f;
            out += hex[i];
            }
        return out;
    }


	@Override
	public void onNewIntent(Intent intent) {
		setIntent(intent);
		resolveIntent(intent);
	}

	class AllDealsAdapter extends BaseAdapter {

		public int getCount() {
			return dealTagsList == null ? 0 : dealTagsList.size();
		}

		public DealTagEntity getItem(int position) {
			return dealTagsList == null ? null : dealTagsList.get(position);
		}

		public long getItemId(int position) {
			return dealTagsList == null ? Long.MIN_VALUE : Long.parseLong(dealTagsList.get(position).getId());
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			TextView dealsText = new TextView(getBaseContext());
			dealsText.setText(dealTagsList.get(position).getTagMessage());
			return dealsText;
		}

	}
}
